package org.locker.device.controller.admin;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.locker.common.idempotent.annotation.RepeatSubmit;
import org.locker.common.log.annotation.Log;
import org.locker.common.web.core.BaseController;
import org.locker.common.mybatis.core.page.PageQuery;
import org.locker.common.core.domain.R;
import org.locker.common.core.validate.AddGroup;
import org.locker.common.core.validate.EditGroup;
import org.locker.common.log.enums.BusinessType;
import org.locker.common.excel.utils.ExcelUtil;
import org.locker.domain.vo.DeviceVersionVo;
import org.locker.domain.bo.DeviceVersionBo;
import org.locker.service.IDeviceVersionService;
import org.locker.common.mybatis.core.page.TableDataInfo;

/**
 * 设备升级信息
 *
 * @author winnie
 * @date 2023-10-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/device/deviceVersion")
public class DeviceVersionController extends BaseController {

    private final IDeviceVersionService deviceVersionService;

    /**
     * 查询设备升级信息列表
     */
    @SaCheckPermission("device:deviceVersion:list")
    @GetMapping("/list")
    public TableDataInfo<DeviceVersionVo> list(DeviceVersionBo bo, PageQuery pageQuery) {
        return deviceVersionService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出设备升级信息列表
     */
    @SaCheckPermission("device:deviceVersion:export")
    @Log(title = "设备升级信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DeviceVersionBo bo, HttpServletResponse response) {
        List<DeviceVersionVo> list = deviceVersionService.queryList(bo);
        ExcelUtil.exportExcel(list, "设备升级信息", DeviceVersionVo.class, response);
    }

    /**
     * 获取设备升级信息详细信息
     *
     * @param deviceVersionId 主键
     */
    @SaCheckPermission("device:deviceVersion:query")
    @GetMapping("/{deviceVersionId}")
    public R<DeviceVersionVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long deviceVersionId) {
        return R.ok(deviceVersionService.queryById(deviceVersionId));
    }

    /**
     * 新增设备升级信息
     */
    @SaCheckPermission("device:deviceVersion:add")
    @Log(title = "设备升级信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DeviceVersionBo bo) {
        return toAjax(deviceVersionService.insertByBo(bo));
    }

    /**
     * 修改设备升级信息
     */
    @SaCheckPermission("device:deviceVersion:edit")
    @Log(title = "设备升级信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DeviceVersionBo bo) {
        return toAjax(deviceVersionService.updateByBo(bo));
    }

    /**
     * 删除设备升级信息
     *
     * @param deviceVersionIds 主键串
     */
    @SaCheckPermission("device:deviceVersion:remove")
    @Log(title = "设备升级信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deviceVersionIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] deviceVersionIds) {
        return toAjax(deviceVersionService.deleteWithValidByIds(List.of(deviceVersionIds), true));
    }


    /**
     * 推送更新
     * @return
     */
    @SaCheckPermission("device:deviceVersion:query")
    @Log(title = "设备升级信息", businessType = BusinessType.OTHER)
    @PostMapping("/pushVersionUpdate")
    public R<Void> pushVersionUpdate() {
        deviceVersionService.pushVersionUpdate();

        return toAjax(true);
    }

}
